package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.OrderCheck;
import com.lkd.emq.Topic;
import com.lkd.entity.OrderEntity;
import com.lkd.exception.LogicException;
import com.lkd.service.OrderService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
@Topic(TopicConfig.ORDER_CHECK_TOPIC)
public class CheckPayHandler implements MsgHandler {
    @Autowired
    OrderService orderService;

    @Override
    public void process(String jsonMsg) throws IOException {
        log.info("接受到消息");
        OrderCheck check = JsonUtil.getByJson(jsonMsg, OrderCheck.class);
        if (check == null || check.getInnerCode() == null || check.getOrderNo() == null) {
            log.info("协议类不完整，缺失信息");
            throw new LogicException("协议类不完整，缺失信息");
        }
        //获取对应订单
        OrderEntity order = orderService.getByOrderNo(check.getOrderNo());
        if(order == null) return;
        if(order.getStatus().equals(VMSystem.ORDER_STATUS_CREATE)){  //如果
            log.info("订单无效处理 订单号：{}",order.getOrderNo());
            order.setStatus(VMSystem.ORDER_STATUS_INVALID); //无效状态
            orderService.updateById(order);
        }

    }
}
